using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using AppPriceLive.Entities.Models;
using AppPriceLive.Entities.Utils;
using AppPriceLive.Common;

namespace AppPriceLive.DAL.Base
{
	public class CurrencyListDAOBase
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors

		public CurrencyListDAOBase(string connectionStringName)
		{
			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the CurrencyList table.
		/// </summary>
		public virtual void Insert(CurrencyListInfo currencyListInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@CurrencyGroupId", currencyListInfo.CurrencyGroupId),
				new SqlParameter("@VName", currencyListInfo.VName),
				new SqlParameter("@EName", currencyListInfo.EName),
				new SqlParameter("@OrderBy", currencyListInfo.OrderBy)
			};

			currencyListInfo.CurrenListId = (int) SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "CurrencyList_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the CurrencyList table.
		/// </summary>
		public virtual void Update(CurrencyListInfo currencyListInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@CurrenListId", currencyListInfo.CurrenListId),
				new SqlParameter("@CurrencyGroupId", currencyListInfo.CurrencyGroupId),
				new SqlParameter("@VName", currencyListInfo.VName),
				new SqlParameter("@EName", currencyListInfo.EName),
				new SqlParameter("@OrderBy", currencyListInfo.OrderBy)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CurrencyList_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the CurrencyList table by its primary key.
		/// </summary>
		public virtual void Delete(int? currenListId)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@CurrenListId", currenListId)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CurrencyList_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the CurrencyList table.
		/// </summary>
		public virtual CurrencyListInfo Select(int? currenListId)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@CurrenListId", currenListId)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CurrencyList_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeCurrencyListInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the CurrencyList table.
		/// </summary>
		public List<CurrencyListInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CurrencyList_SelectAll"))
			{
				List<CurrencyListInfo> currencyListInfoList = new List<CurrencyListInfo>();
				while (dataReader.Read())
				{
					currencyListInfoList.Add(MakeCurrencyListInfo(dataReader));
				}

				return currencyListInfoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the CurrencyList class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual CurrencyListInfo MakeCurrencyListInfo(SqlDataReader dataReader)
		{
			CurrencyListInfo currencyListInfo = new CurrencyListInfo();
			currencyListInfo.CurrenListId = SqlClientUtility.GetInt32(dataReader, DbConstants.CURRENCYLIST.CURRENLISTID);
			currencyListInfo.CurrencyGroupId = SqlClientUtility.GetInt32(dataReader, DbConstants.CURRENCYLIST.CURRENCYGROUPID);
			currencyListInfo.VName = SqlClientUtility.GetString(dataReader, DbConstants.CURRENCYLIST.VNAME);
			currencyListInfo.EName = SqlClientUtility.GetString(dataReader, DbConstants.CURRENCYLIST.ENAME);
			currencyListInfo.OrderBy = SqlClientUtility.GetInt32(dataReader, DbConstants.CURRENCYLIST.ORDERBY);

			return currencyListInfo;
		}

		#endregion
	}
}
